package hussein.musicstore.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class FlyTunes
{
	private HashMap<String, Album> albums = new HashMap<String, Album>();
	private ArrayList<Song> songs = new ArrayList<Song>();

	private void listSongs()
	{
		for (int i = 0; i < songs.size(); i++)
		{
			songs.get(i);
		}

	}

	private void listSongsInAlbum(String name) throws InvalidRequestException
	{
		if (albums.containsKey(name))
		{
			albums.get(name).getSongs().next();

		} else
		{
			throw new InvalidRequestException("The album " + name
					+ "doesn't exist");
		}
	}

	private void listAlbums()
	{
		Iterator<String> albumI = albums.keySet().iterator();
		while (albumI.hasNext())
		{
			albums.get(albumI.next());

		}
	}

	private int findSong(String name, String band)
	{
		int index = -1;
		for (int i = 0; i < songs.size(); i++)
		{
			if (songs.get(i).getSongName().equals(name)
					&& (songs.get(i).getBandName().equals(band)))

			{
				index = i;
				break;

			}
		}

		return index;

	}

	private Song addSong(String name, String band, double price) 
	{
		if (name.equals(""))
			return null;
		int songIndex = findSong(name, band);

		if (songIndex != -1)
		{
			try
			{
				throw new InvalidRequestException("The song " + name
						+ "already exists");
			} catch (InvalidRequestException e)
			{
				e.printStackTrace();
			}
		
			return songs.get(songIndex);
		}

		 else
		{
			Song song = new Song(name, band, price);
			songs.add(song);
			return song;
		}
	}

	private void addAlbum(String name, String band)
	{
		if (albums.containsKey(name))
		{
			try
			{
				throw new InvalidRequestException("The Album: " + name + " is already in Store");
			} catch (InvalidRequestException e)
			{
				e.printStackTrace();
			}
		} else
		{
			Album album = new Album(name, band);
			albums.put(name, album);
			
		}
		// println("New album added to Store");
	}
	
	private void addSongToAlbum(String nameofSong, String albumName, double price)
	{
		if(albums.containsKey(albumName))
			
		while (true)
		{
			Song song = addSong(nameofSong, bandName, price);
			if (song == null)
				break;
			album.AddSong(song);
		}
		
	}

	private void updateSongPrices()
	{
		// String name = readLine("Name: ");
		// String band = readLine("Band: ");

		int songIndex = findSong(name, band);
		if (songIndex == -1)
		{
			// println("The song " +name+ "is NOT available in Store");

		} else
		{
			// double price = readDouble("New price : ");
			songs.get(songIndex).setSongPrice(price);
			// println("Price for song "+name+ "has been updated");

		}

	}

}
